package com.room.controller.repaire;

import com.room.pojo.DormResult;
import com.room.pojo.Repairitems;
import com.room.pojo.Repairlog;
import com.room.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/repair/manager")
public class RepairController {
    @Autowired
    private RepairService repairService;

    @RequestMapping(value = "/items",method = RequestMethod.GET)
    public Map<String, Object> findAllItems(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "6") Integer size) {
        Map<String, Object> map = new HashMap<>();
        List<Repairitems> repairitmes = repairService.findAllTbRepairitmes(page, size);
        int total = repairService.getRepairitemsCount();
        map.put("items", repairitmes);
        map.put("total", total);
        return map;
    }

    @RequestMapping(value = "/items",method = RequestMethod.POST)
    public DormResult additems( Repairitems repairitmes) {
        repairitmes.setReptime(new Date());
        int flag = repairService.addRepairitems(repairitmes);
        if(flag==1){
            return DormResult.ok("报修成功");
        }
        return DormResult.ok("报修失败");
    }

    @RequestMapping(value = "/item/{item_ids}",method = RequestMethod.DELETE)
    public DormResult delete(@PathVariable String item_ids) {
        String[] split = item_ids.split(",");
        int item_id = Integer.parseInt(split[0]);
                String workId = split[1];
        int flag = repairService.deleteRepairitems(item_id, workId);
        if(flag==1){
            return DormResult.ok("删除成功");
        }
        return DormResult.ok("删除失败");
    }

    @PutMapping("/items")
    @ResponseBody
    public DormResult update(@RequestBody Repairitems repairitmes) {
        repairitmes.setReptime(new Date());
        int flag = repairService.updateRepairitems(repairitmes);
        if(flag==1){
            return DormResult.ok("修改成功");
        }
        return DormResult.ok("修改失败");
    }

    @RequestMapping(value = "/log",method = RequestMethod.GET)
    public Map<String, Object> findAllLog(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "6") Integer size) {
        Map<String, Object> map = new HashMap<>();
        List<Repairlog> allRepairlog = repairService.findAllRepairlog(page, size);
        int total = repairService.getRepairlogCount();
        map.put("log", allRepairlog);
        map.put("total", total);
        return map;
    }


    @RequestMapping(value = "/logbyWorkNo",method = RequestMethod.GET)
    public Map<String, Object> findlogbyworkerid(@RequestParam String workerNo,@RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "6") Integer size) {
        Map<String, Object> map = new HashMap<>();
        List<Repairlog> repairlogByWorkerId = repairService.findRepairlogByWorkerId(workerNo,page, size);
        int total = repairService.getRepaitlogCountByWorkerId(workerNo);
        map.put("log", repairlogByWorkerId);
        map.put("total", total);
        return map;
    }

    @GetMapping("/itemsbr")
    @ResponseBody
    public Map<String, Object> finditemsbyroomName(@RequestBody Map<String,Object> m,@RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "6") Integer size) {
        Map<String, Object> map = new HashMap<>();
        String roomName = (String) m.get("roomName");
        List<Repairitems> repairitmeByRoomName = repairService.findRepairitmeByRoomName(roomName,page,size);
        int total = repairService.getRepairitemsCountByRoomName(roomName);
        map.put("items", repairitmeByRoomName);
        map.put("total", total);
        return map;
    }
}